package com.software.aperture.controller;

import com.software.aperture.bean.HttpResult;
import com.software.aperture.bean.ResultCode;
import com.software.aperture.bean.User;
import com.software.aperture.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("userLogin")
public class LoginController {
    @Autowired
    private UserService userService ;
    private static User currentUser;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public HttpResult login(@RequestBody  User user){
        if(user.getAccountName() == null){

        }else if(user.getPassword() == null){

        }else{
            System.out.println("accountName:"+user.getAccountName());
            currentUser = getUserByAccountName(user.getAccountName(),user.getPassword());
            if(userService. findUserByAccountName(user.getAccountName()) != null){

            }
        }

        return new HttpResult(ResultCode.NO_ERROR, "success");
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public HttpResult Register(@RequestBody  User user){
        if(user.getAccountName() == null){
            return new HttpResult(ResultCode.NULL_USERNAME_OR_PASSWORD_ERROR,"error");
        }else if(user.getPassword() == null){

        }else{
            userService.addUser(user);
        }

        return new HttpResult(ResultCode.NO_ERROR, "success");
    }


    @RequestMapping(value = "/getUserByAccountName", method = RequestMethod.GET)
    public User getUserByAccountName(@RequestParam(value = "accountName") String accountName,
                                     @RequestParam(value = "password") String password){
        System.out.println("by accountName: " + accountName);
       User user = userService. findUserByAccountName(accountName);
        currentUser = userService.findUserByAccountName(accountName);
        System.out.println("currentUser.age: "+currentUser.getAge());
        if(password.equals(currentUser.getPassword())) {
            return user;
        }else {
            return null;
        }
    }

    @RequestMapping(value = "/getCurrentUser",method = RequestMethod.GET)
    public User getCurrentUser(){
        if(currentUser.getAccountName() != null) {
            System.out.println("currentUser.accountName: " + currentUser.getAccountName());
        }else {
            System.out.println("currentUser.accountName is null ");
        }
        if(currentUser.getAge() != null) {
            System.out.println("currentUser.age: " + currentUser.getAge());
        }else {
            System.out.println("currentUser.accountName is null ");
        }
        return currentUser;
    }


    @RequestMapping(value = "/editUser",method = RequestMethod.POST)
    public HttpResult editUser(@RequestBody User user){
        if(user.getAccountName() == null){

        }else {
            userService.editUser(user);
        }
        return new HttpResult(ResultCode.NO_ERROR, "success");
    }

}
